﻿using System;
using System.IO;
using System.Text;

namespace CheckCsFilesForKeyword
{
    public class Program
    {
        public static string keyword = "跑路";

        [STAThread]
        public static void Main(string[] args)
        {
            Console.WriteLine("请输入要检查的文件夹路径：");
            string folderPath = Console.ReadLine();
            if (!Directory.Exists(folderPath))
            {
                Console.WriteLine("指定的文件夹不存在！");
                return;
            }

            Console.WriteLine("请输入要检查的关键字：");
            keyword = Console.ReadLine();

            Console.WriteLine($"开始检查文件夹: {folderPath} 及其子文件夹中的.cs文件...\n");
            int totalFiles = 0;
            int filesWithKeyword = 0;

            foreach (string filePath in Directory.EnumerateFiles(folderPath, "*.cs", SearchOption.AllDirectories))
            {
                totalFiles++;
                bool containsKeyword = false;

                try
                {
                    string fileContent = File.ReadAllText(filePath, Encoding.UTF8);
                    containsKeyword = fileContent.IndexOf(keyword, StringComparison.OrdinalIgnoreCase) >= 0;

                    if (containsKeyword)
                    {
                        filesWithKeyword++;
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine($"发现: {filePath} 包含'{keyword}'");
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine($"正常: {filePath}");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"无法读取文件 {filePath}: {ex.Message}");
                    continue;
                }
                finally
                {
                    Console.ResetColor();
                }
            }

            Console.WriteLine("\n检查完成！");
            Console.WriteLine($"共检查 {totalFiles} 个.cs文件");
            Console.WriteLine($"其中 {filesWithKeyword} 个文件包含'{keyword}'关键字");

            if (filesWithKeyword > 0)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"警告：发现包含'{keyword}'关键字的文件！");
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine($"恭喜！未发现包含'{keyword}'关键字的文件。");
            }

            Console.ResetColor();
            Console.WriteLine("\n按任意键退出...");
            Console.ReadKey();
        }
    }
}